// -o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-
// -o   INTRUDERS SECURITY PASSWORD	o 
// -o        (c) 2009 by Myzter         o 
// -o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-
// -o This system detect IP Changes and o
// -o ask a second password.            o
// -o Players that input the wrong pass o
// -o can't enter to the game.          o
// -o                                   o
// -o Include a OnLine Timer (just fun) o
// -o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-

-	script	FirstTimeScript	-1,{
OnPCLoginEvent:
	set .@cnt, query_sql("SELECT l.userid FROM login l where l.account_id = " + getcharid(3),.@LUserID$);
	set .@cnt, query_logsql("SELECT l.ip FROM loginlog l where l.user = '" + .@LUserID$[0] + "' and l.rcode = 100 order by l.time desc limit 1",.@LIp$);
	if (!.@cnt) end;
	// A little online timer (funny)
	dispbottom "* Online Time: " + callfunc("Sec2HHX",#OLTIME);
	callfunc "CheckLoginAuth",.@LIp$[0];
	set #LASTIP$,.@LIp$[0];
	set @GTT_Login,gettimetick(2);
	if (#securitypass$ == "") dispbottom "Warning: Your account does not have a Security Password, please whisp to npc:security to create it.";
	end;
OnPCLogoutEvent:
	if (@NoLogin) end;
	if (!@GTT_Login) end;
	if (checkvending()) end;
	set @LTotalTime, gettimetick(2) - @GTT_Login;
	set #OLTIME, #OLTIME + @LTotalTime;
}

function	script	Sec2HHX	{
	set .@DD, getarg(0) / 86400;
	set .@HH, (getarg(0) - .@DD * 86400) / 3600;
	set .@MM, (getarg(0) - .@DD * 86400 - .@HH * 3600) / 60;
	set .@SS, getarg(0) - .@DD * 86400 - .@HH * 3600 - .@MM * 60;
	set .@RT$, .@DD + " day(s) ";
	if (.@HH < 10) set .@RT$, .@RT$ + "0" + .@HH; else set .@RT$, .@RT$ + .@HH;
	if (.@MM < 10) set .@RT$, .@RT$ + ":0" + .@MM; else set .@RT$, .@RT$ + ":" + .@MM;
	if (.@SS < 10) set .@RT$, .@RT$ + ":0" + .@SS; else set .@RT$, .@RT$ + ":" + .@SS;
	return .@RT$;
}

function	script	CheckLoginAuth	{
	if (#securitypass$ == "") return;
	if (#LASTIP$ == getarg(0) || #LASTIP$ == "") return;
	cutin "mets_alpha.bmp",2;
	mes "[^FF0000Security Assistant^000000]";
	mes "Hi ^0000FF" + strcharinfo(0) + "^000000, please input your security password.";
	mes " ";
	mes "(^ff0000New IP detected!^000000)";
	input @scpass$;
	if (@scpass$ != #securitypass$) {
		set @NoLogin,1;
		atcommand "@die";
		atcommand "@kick " + strcharinfo(0);
		close;
	}
	set @NoLogin,0;
	cutin "",255;
	atcommand "@refresh";
	message strcharinfo(0),"Thank's, enjoy the game!";
	return;
}

-	script	security	-1,{
OnWhisperGlobal:
	if (gettimetick(2) - #Security < 1800) {
		dispbottom "** Error: This service is temporary locked, please try again later.";
		end;
	}
l_Start:
	cutin "mets_alpha.bmp",2;
	mes "[^FF0000Security Assistant^000000]";
	mes "Hi ^0000FF" + strcharinfo(0) + "^000000, what can i do for you?";
	next;
	switch(select("^0000FFSet Security Password^000000","^FF0000Help^000000","Exit")) {
		case 1:
			l_SetKaf:
			mes "[^FF0000Security Assistant^000000]";
			if (#securitypass$ == "") {
				mes "There's no security password in this account:";
			} else {
				mes "Your security password is active, please confirm it now:";
				next;
				input .@secstr$;
				mes "[^FF0000Security Assistant^000000]";
				if (.@secstr$ != #securitypass$) {
					set #SecurityTry, #SecurityTry + 1;
					if (#SecurityTry > 2) {
						set #Security,gettimetick(2);
						set #SecurityTry,0;
						mes "Error, try to remember your password before use this service.";
						mes " ";
						mes "[^ff0000Service locked 30 min.^000000]";
						close2;
						goto l_Exit;
					}
					mes "^ff0000Access denied!^000000 the password is wrong!";
					mes " ";
					mes "[^ff0000" + #SecurityTry + " / 3^000000]";
					next;
					goto l_SetKaf;
				}
				mes "Affirmative, input your new security password:";
			}
			l_InputPass:
			mes " ";
			mes "(6-20 chars, type \"^ff00000^000000\" to leave empty)";
			next;
			input .@secstr$;
			mes "[^FF0000Security Assistant^000000]";
			if (.@secstr$ == "0") {
				set #securitypass$, "";
				mes "This account have not security password, please set a password whispering to ^ff0000npc:security^000000.";
				close2;
				set #Security,0;
				set #SecurityTry,0;
				goto l_Exit;
			}
			if (getstrlen(.@secstr$) < 6 || getstrlen(.@secstr$) > 20) {
				mes "^ff0000Invalid password!^000000, please check the length and try again.";
				goto l_InputPass;
			}
			mes "The new security password is:";
			mes " ";
			mes "^c0c0c0" + .@secstr$ + "^000000";
			next;
			if (select("^0000ffDone!^000000","^ff0000Change!^000000") == 2) {
				mes "[^FF0000Security Assistant^000000]";
				mes "Please input your new security password:";
				goto l_InputPass;
			}
			set #securitypass$, .@secstr$;
			set #Security,0;
			set #SecurityTry,0;
			mes "[^FF0000Security Assistant^000000]";
			mes "Your new security password it's active, please keep a backup in a security place.";
			close2;
			goto l_Exit;
		case 2:
			mes "[^FF0000Security Assistant^000000]";
			mes "The security password is a ^0000ffsecond password^000000 that protect your account from intruders trying to access from another IP Address.";
			next;
			mes "[^FF0000Security Assistant^000000]";
			mes "Your IP address almost does not change, while the other players will have always a different IP, triggering the security check.";
			next;
			mes "[^FF0000Security Assistant^000000]";
			if (#securitypass$ == "") {
				mes "Please protect your account with the security password!";
				next;
			} else {
				mes "Remember, don't share your passwords!";
				next;
			}
			goto l_Start;
	}
	mes "[^FF0000Security Assistant^000000]";
	mes "See you soon.";
	close2;
l_Exit:
	cutin "",255;
}